//
//  LoginViewController.swift
//  maosheng
//
//  Created by 何海涛 on 2019/8/10.
//

import Foundation
import UIKit
import SnapKit
class LoginViewController: UIViewController, LoginViewDelegate, LoginInputViewDelegate {
    
    var loginView: LoginView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        loginView = LoginView()
        self.view.addSubview(loginView)
        self.view.backgroundColor = UIColor.white
        self.fd_prefersNavigationBarHidden = true
        loginView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        loginView.render()
        loginView.delegate = self
        loginView.inputCode.delegate = self
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    func sendValidateCode() {
        // todo: send get validate code request
        let phoneNum = loginView!.inputMobile.txt.text!
        let isValid = loginView.inputCode.checkPhoneValid(phoneNum: phoneNum)
        if isValid{
            SDataCenter.shared.sendVertifyCode(type: 2, phoneNum: phoneNum) { [weak self](isSuccess) in
                if isSuccess{
                    self?.loginView.inputCode.beginCoundown()
                }
            }
        }
    }
    
    func sendLogin(_ model: ValidationCodeLoginRequest) {
        // todo: send login request
        ProgressHUD.show("登录中...", in: self.view, mode: ProgressModeLoading)
        var params = [String : Any]()
        var str_url = ""
        if loginView.currentMode == .validate {
            str_url = "api/TokenAuth/AuthenticateMobile"
            params["phoneNumber"] = loginView!.inputMobile.txt.text!
            params["securityCode"] = loginView!.inputCode.txt.text!

        }else{
            str_url = "api/TokenAuth/Authenticate"
            params["userNameOrEmailAddress"] = loginView!.inputMobile.txt.text!
            params["password"] = loginView!.inputCode.txt.text!

        }
        SDataCenter.shared.SPostJsonDataWithUrl(str_url: str_url, params: params) { (responseData, isSuccess) in
            ProgressHUD.hide()
            if isSuccess{
                if let dict = responseData as? [String : Any]{
                    if let token = dict["accessToken"] as? String{
                        PLUserDefult.set(token, forKey: SUserToken)
                        if let app = UIApplication.shared.delegate as? AppDelegate{
                            app.createRootViewController()
                        }
                    }
                }
            }
        }
    }
    
    func jumpFindPasswordPage(isRegist : Bool) {
        let findPwdCtrl = FindPasswordViewController()
        findPwdCtrl.isRegist = isRegist
        navigationController?.pushViewController(findPwdCtrl, animated: true)
    }
    
    func jumpUserProtocolPage() {
        // todo: jump h5 user protocol
    }
    
    func sendWechatLogin() {
        // todo: wechat login
    }
}
